package cs5226.actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

import cs5226.dao.ConfigurationDAO;
import cs5226.model.AwrSetting;
import cs5226.model.ThresholdBean;
import cs5226.util.Constants;
import cs5226.util.StringUtil;

public class ConfigThresholdsAction  extends ActionSupport implements  ServletRequestAware {
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	static Log log = LogFactory.getLog(ConfigThresholdsAction.class);	
	
	private HttpServletRequest request = null;

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}
	
	public String getThresholds() throws Exception {
		try {
			
			request.setAttribute("thresholdsList", ConfigurationDAO.getThresholds());
			
			
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("error", e.getMessage());
		}

		return super.SUCCESS;
	}
	
	public String updateThresholds() throws Exception {		
		try {
			ArrayList<ThresholdBean> aList = ConfigurationDAO.getThresholds();
			int counter=0;
			
			for(ThresholdBean bean : aList){
				bean.setMin(Double.parseDouble(request.getParameter(bean.getParamID()+"_min")));
				bean.setUnhealthy(Double.parseDouble(request.getParameter(bean.getParamID()+"_unhealthy")));
				bean.setCritical(Double.parseDouble(request.getParameter(bean.getParamID()+"_critical")));
				bean.setMax(Double.parseDouble(request.getParameter(bean.getParamID()+"_max")));
				
				aList.set(counter, bean);
				counter++;
			}
			
			;
			request.setAttribute("thresholdsList", ConfigurationDAO.updateThresholds(aList));
			//request.setAttribute("thresholdsList", ConfigurationDAO.getThresholdsTest()); 
			request.setAttribute("msg", "Thresholds have been updated successfully. ");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", "Exception when update Threshold. "+e.getMessage());
			return super.ERROR;					
		}

		return super.SUCCESS;
	}

}